<script type="text/javascript">
	var url_local = "${paramTool.url_local}";

	// 初始化
	jQuery(function() {
		// 用户grid
		create_database_grid();
	});

	function create_database_grid() {
		var sort = jQuery(".data_operate_bar [name='fieldSort']").val();
		gxc.grid.create({
			name : "database_grid",
			url : url_local + "/datacenter/database/grid.do",
			withCheckbox : true,
			withRownum : false,
			stripeRows : false,
			stripeOverRow : false,
			withPagebar : true,
			pageSize : 10,
			renderTo : "#database_grid",
			fieldSort : sort,
			loadDataNow : true,
			columns : [ {
				text : "数据源名称",
				name : "db_name"
			}, {
				text : "数据源类型",
				name : "db_type"
			}, {
				text : "主机",
				name : "db_host"
			}, {
				text : "端口",
				name : "db_port"
			}, {
				text : "操作",
				name : "id",
				width : "200px",
				className : "td_operate",
				align : "center",
				render : renderOperate
			} ]
		});
	}

	function renderOperate(item) {
		var operate = "<a  href='javascript: view_database(\"" + item.id
				+ "\")'>查看详细</a>";
		operate += "<a style='margin-left:5px;' href='javascript: edit_database(\""
				+ item.id + "\")'>编辑</a>";
		operate += "<a style='margin-left:5px;' href='javascript: delete_database(\""
				+ item.id + "\")'>删除</a>";
		operate += "<a style='margin-left:5px;' href='javascript: view_tables(\""
				+ item.id + "\")'>查看数据表</a>";
		return operate;
	}

	function query_data() {
		gxc.grid.database_grid.params = null;
		gxc.grid.database_grid.params = gxc.form.getParams("#frm");
		gxc.grid.database_grid.pageCurrent = 1;
		gxc.grid.database_grid.reload();
	}

	function reload() {
		gxc.grid.database_grid.reload();
	}

	function deleteSelected() {
		var id_array = gxc.grid.database_grid.getSelectedRows().toString();
		if (id_array == "") {
			return;
		}
		jQuery.ajax({
			type : "post",
			url : url_local + "/datacenter/database/deleteBatch.do",
			data : {
				id : id_array
			},
			dataType : "json",
			beforeSend : function(XMLHttpRequest) {
			},
			success : function(data, textStatus) {
				if (data.status == "success") {
					gxc.dialog.prompt("成功删除 " + data.data + " 条记录！");
					gxc.grid.database_grid.reload();
				}
			},
			complete : function(XMLHttpRequest, textStatus) {
			},
			error : function() {
			}
		})
	}

	function view_database(id) {
		window.open(url_local + "/datacenter/database/view.do?id=" + id,
				"_blank");
	}

	function edit_database(id) {
		window.open(url_local + "/datacenter/database/edit.do?id=" + id,
				"_blank");
	}

	function delete_database(id) {
		jQuery.ajax({
			type : "post",
			url : url_local + "/datacenter/database/delete.do",
			data : {
				id : id
			},
			dataType : "json",
			beforeSend : function(XMLHttpRequest) {
			},
			success : function(data, textStatus) {
				if (data.status == "success") {
					gxc.dialog.prompt("成功删除 " + data.data + " 条记录！");
					gxc.grid.database_grid.reload();
				}
			},
			complete : function(XMLHttpRequest, textStatus) {
			},
			error : function() {
			}
		})
	}

	function view_tables(id) {
		window.open(url_local + "/datacenter/table/list.do?database_id=" + id,
				"_blank");
	}

	function changeFieldSort() {
		gxc.grid.database_grid.fieldSort = jQuery("[name='fieldSort']").val();
		gxc.grid.database_grid.page_current = 1;
		gxc.grid.database_grid.reload();
	}
</script>
